﻿/*                              
   Copyright 2011 Nils Kopal, Christopher Krüger, Simone Sauer, Dennis Nolte, Uni Duisburg-Essen

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using Sharebert.LinkLayerLibrary.API;
using Sharebert.OverlayLibrary.API;
using Sharebert.OverlayLibrary.Messages;
using Sharebert.LinkLayerLibrary;

namespace Sharebert.OverlayLibrary
{
    /// <summary>
    /// ConnectionHandler provides an easy to use interface to reply to received messages.
    /// </summary>
    public class ConnectionHandler : IConnectionHandler
    {

        /// <summary>
        /// Initializes a new instance of the ConnectionHandler class.
        /// </summary>
        /// <param name="conn">The IConnection to the remote peer</param>
        public ConnectionHandler(IConnection conn)
        {
            if (conn == null)
            {
                throw new ArgumentNullException("Connection can not be null");
            }
            this.connection = conn;
        }

        /// <summary>
        /// Sends a message to the peer with the given recipientID and the given payload.
        /// </summary>
        /// <param name="senderID">PeerID of the sender</param>
        /// <param name="recipientID">PeerID of the recipient</param>
        /// <param name="messageType">The message type</param>
        /// <param name="payload">The payload of this message</param>
        /// <exception cref="System.ArgumentException">Argument is null</exception>
        /// <exception cref="Sharebert.NetworkLayerLibrary.SerializationException">Serialization of the search results failed</exception>
        /// <exception cref="Sharebert.NetworkLayerLibrary.NetworkException">Error while sending the search results</exception>
        public void SendMessage(byte[] senderID, byte[] recipientID, string messageType, byte[] payload)
        {
            if (senderID == null || recipientID == null || payload == null || String.IsNullOrWhiteSpace(messageType))
            {
                throw new ArgumentException();
            }
            Header header = new Header(messageType, payload.Length);
            Message message = new Message(header, payload);
            this.connection.SendMessage(message);
        }

        /// <summary>
        /// Closes the underlying connection.
        /// </summary>
        public void Disconnect()
        {
            this.connection.Close();
        }

        /// <summary>
        /// Returns the remote [IP]:[Port]
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.RemoteIP + ":" + this.RemotePort;
        }


        #region Properties and private member

        /// <summary>
        /// The IP address of the remote client.
        /// </summary>
        public string RemoteIP { get { return this.connection.RemoteAddress; } }

        /// <summary>
        /// The port of the remote client.
        /// </summary>
        public int RemotePort { get { return this.connection.RemotePort; } }

        /// <summary>
        /// The IP address of the local client.
        /// </summary>
        public string LocalIP { get { return this.connection.LocalAddress; } }

        /// <summary>
        /// The port of the local client.
        /// </summary>
        public int LocalPort { get { return this.connection.LocalPort; } }

        private IConnection connection;

        #endregion

    }
}
